//
//  addDetailshopTableViewCell.m
//  YiFei
//
//  Created by yangyan on 16/8/30.
//  Copyright © 2016年 yous. All rights reserved.
//

#import "addDetailshopTableViewCell.h"
#import "BUYButton.h"
#import "DownListButton.h"
#import "PureLayout.h"
#import "DownListBgView.h"
@interface addDetailshopTableViewCell()<DownListButtonDelegate, DownListBgViewDelegate>
@property (nonatomic, strong) DownListButton  *moneyDownListBtn;
@property (nonatomic, strong) DownListButton  *priceDownListBtn;
@property (nonatomic, strong) DownListBgView  *moneyDownListBtnBgView;
@property (nonatomic, strong) DownListBgView  *priceDownListBtnBgView;
@end
@implementation addDetailshopTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addView];
    }
    return self;
}

-(void)addView{
    
    self.letfArray=@[@"商品颜色",@"商品价格",@"货币类型",@"价格条款"];
    for (int i=0; i<self.letfArray.count; i++) {
        self.lable=[[UILabel alloc] init];
        self.lable.textColor=[UIColor blackColor];
        [self sizeToFit];
        self.lable.font=[UIFont systemFontOfSize:14.0];
        self.lable.textAlignment=NSTextAlignmentCenter;
        self.lable.frame=CGRectMake(0, 5+40*i, 100, 40);
        self.lable.text=self.letfArray[i];
        [self.contentView addSubview:self.lable];
        
        UIView *lineV=[[UIView alloc] init];
        [lineV sizeToFit];
        lineV.backgroundColor=[UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1.0];
        lineV.frame=CGRectMake(0, CGRectGetMaxY(self.lable.frame), WIDTH, 1);
        [self.contentView addSubview:lineV];
    }
    
    _moneyDownListBtn = [[DownListButton alloc] initForAutoLayout];
    _moneyDownListBtn.btnTitleLabel.text = @"美元";
    [self.contentView addSubview:_moneyDownListBtn];
    [self.moneyDownListBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:CGRectGetMaxX(self.lable.frame)];
    [self.moneyDownListBtn autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:85];
    [self.moneyDownListBtn autoSetDimension:ALDimensionWidth toSize:100];
    [self.moneyDownListBtn autoSetDimension:ALDimensionHeight toSize:30];
    self.moneyDownListBtn.delegate = self;
    
    
    _priceDownListBtn = [[DownListButton alloc] initForAutoLayout];
    _priceDownListBtn.btnTitleLabel.text = @"EXW";
    [self.contentView addSubview:self.priceDownListBtn];
    [self.priceDownListBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:CGRectGetMaxX(self.lable.frame)];
    [self.priceDownListBtn autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:130];
    [self.priceDownListBtn autoSetDimension:ALDimensionWidth toSize:100];
    [self.priceDownListBtn autoSetDimension:ALDimensionHeight toSize:30];
    self.priceDownListBtn.delegate = self;

    
}





#pragma mark DownListButtonDelegate
- (void)showList:(DownListButton *)downListBtn {
    if (downListBtn == self.moneyDownListBtn) {
        [self rightBClick:nil];
    } else if (downListBtn == self.priceDownListBtn) {
        [self rightB2Click:nil];
    }
}

#pragma mark DownListBgViewDelegate
- (void)dismissDownListView:(DownListBgView *)downListView {
    if (downListView == self.priceDownListBtnBgView) {
        [self.priceDownListBtnBgView removeFromSuperview];
    } else if (downListView == self.moneyDownListBtnBgView) {
        [self.moneyDownListBtnBgView removeFromSuperview];
    }
}

-(void)rightBClick:(UIButton *)button{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    _titleArray=[NSArray arrayWithObjects:@"美元",@"欧元",@"人民币",@"英镑",@"日元", nil];
    CGRect frame = [self convertRect:self.moneyDownListBtn.frame toView:window];
    self.moneyDownListBtnBgView = [[DownListBgView alloc] initWithFrame:window.bounds];
    self.moneyDownListBtnBgView.delegate = self;
    self.backView=[[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.lable.frame), frame.size.height + frame.origin.y, frame.size.width, 31*_titleArray.count)];
    [window addSubview:self.moneyDownListBtnBgView];
    [self.moneyDownListBtnBgView addSubview:self.backView];
    self.backView.backgroundColor = [UIColor whiteColor];
    self.backView.layer.borderWidth = 1;
    self.backView.layer.cornerRadius = 5;
    self.backView.layer.borderColor = NAVCOLOR.CGColor;
    for (int i=0; i<_titleArray.count; i++) {
        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 30*i, WIDTH-CGRectGetMaxX(self.lable.frame)-170, 30)];
        [btn setTitle:_titleArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font=[UIFont systemFontOfSize:14.0];
        [btn.titleLabel setTextAlignment:NSTextAlignmentCenter];
        [btn setTitleColor:NAVCOLOR forState:UIControlStateNormal];
        btn.tag=i+100;
        [btn addTarget:self action:@selector(HuoBiClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn.layer setBorderWidth:0];
        [self.backView addSubview:btn];
        
        UIView *viewx = [[UIView alloc]initWithFrame:CGRectMake(0,31*i, self.backView.frame.size.width, 1)];
        [self.backView addSubview:viewx];
        viewx.backgroundColor = NAVCOLOR;
    }
}

-(void)HuoBiClick:(UIButton *)sender{
    switch (sender.tag) {
        case 100:
            [self.moneyDownListBtn.btnTitleLabel setText:@"美元"];
            _shopObject.shopHuoBi=@"美元";
            self.backView.hidden=YES;
            break;
        case 101:
            [self.moneyDownListBtn.btnTitleLabel setText:@"欧元"];
            _shopObject.shopHuoBi=@"欧元";
            self.backView.hidden=YES;
            break;
        case 102:
            [self.moneyDownListBtn.btnTitleLabel setText:@"人民币"];
            _shopObject.shopHuoBi=@"人民币";
            self.backView.hidden=YES;
            break;
            
        case 103:
            [self.moneyDownListBtn.btnTitleLabel setText:@"英镑"];
            _shopObject.shopHuoBi=@"英镑";
            self.backView.hidden=YES;
            break;
            
        case 104:
            [self.moneyDownListBtn.btnTitleLabel setText:@"日元"];
            _shopObject.shopHuoBi=@"日元";
            self.backView.hidden=YES;
            break;
            
        default:
            break;
    }
    
}


-(void)rightB2Click:(UIButton *)button{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    _titleArray2=[NSArray arrayWithObjects:@"EXW",@"FAS",@"FOB",@"FCA", nil];
    CGRect frame = [self convertRect:self.priceDownListBtn.frame toView:window];
    self.priceDownListBtnBgView = [[DownListBgView alloc] initWithFrame:window.bounds];
    self.priceDownListBtnBgView.delegate = self;
    self.backView2=[[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.lable.frame), frame.size.height + frame.origin.y, frame.size.width, 31*_titleArray2.count)];
    [self.priceDownListBtnBgView addSubview:self.backView2];
    [window addSubview:self.priceDownListBtnBgView];
    self.backView2.backgroundColor = [UIColor whiteColor];
    self.backView2.layer.borderWidth = 1;
    self.backView2.layer.cornerRadius = 5;
    self.backView2.layer.borderColor = NAVCOLOR.CGColor;
    for (int i=0; i<_titleArray2.count; i++) {
        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 30*i, WIDTH-CGRectGetMaxX(self.lable.frame)-170, 30)];
        [btn setTitle:_titleArray2[i] forState:UIControlStateNormal];
        [btn.titleLabel setTextAlignment:NSTextAlignmentCenter];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font=[UIFont systemFontOfSize:14.0];
        [btn setTitleColor:NAVCOLOR forState:UIControlStateNormal];
        btn.tag=i+200;
        [btn addTarget:self action:@selector(PriceButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn.layer setBorderWidth:0];
        [self.backView2 addSubview:btn];
        
        UIView *viewx = [[UIView alloc]initWithFrame:CGRectMake(0,31*i, self.backView2.frame.size.width, 1)];
        [self.backView2 addSubview:viewx];
        viewx.backgroundColor = NAVCOLOR;
    }
}




-(void)PriceButtonClick:(UIButton *)sender{
    switch (sender.tag) {
        case 200:
            [self.priceDownListBtn.btnTitleLabel setText:@"EXW"];
            _shopObject.shopTiaoK=@"EXW";
            self.backView2.hidden=YES;
            break;
        case 201:
            [self.priceDownListBtn.btnTitleLabel setText:@"FAS"];
            _shopObject.shopTiaoK=@"FAS";

            self.backView2.hidden=YES;
            break;
        case 202:
            [self.priceDownListBtn.btnTitleLabel setText:@"FOB"];
            _shopObject.shopTiaoK=@"FOB";

            self.backView2.hidden=YES;
            break;
        case 203:
            [self.priceDownListBtn.btnTitleLabel setText:@"FCA"];
            _shopObject.shopTiaoK=@"FCA";

            self.backView2.hidden=YES;
            break;
        default:
            break;
    }
    
}




-(void)setShopObject:(shopData *)shopObject{
    [self.rightB2 setTitle:@"FCA" forState:UIControlStateNormal];
    [self.rightB setTitle:@"人民币" forState:UIControlStateNormal];

        if (shopObject.shopTiaoK.length==0) {
            shopObject.shopTiaoK=@"FCA";
        }else{
            
            for (int i=0; i<_titleArray2.count; i++) {
                UIButton *button = [self.contentView viewWithTag:200 + i];
                switch (i) {
                    case 0:
                        [button setTitle:shopObject.shopTiaoK forState:UIControlStateNormal];
                        break;
                    case 1:
                        [button setTitle:shopObject.shopTiaoK forState:UIControlStateNormal];
                        break;
                    case 2:
                        [button setTitle:shopObject.shopTiaoK forState:UIControlStateNormal];
                        break;
                    case 3:
                        [button setTitle:shopObject.shopTiaoK forState:UIControlStateNormal];
                        break;
                        
                    default:
                        break;
                }
            }
        }
    
    if (shopObject.shopHuoBi.length==0) {
        shopObject.shopHuoBi=@"人民币";
    }else{
        for (int j=0; j<_letfArray.count; j++) {
            UIButton *button = [self.contentView viewWithTag:100 + j];
            switch (j) {
                case 0:
                    [button setTitle:shopObject.shopHuoBi forState:UIControlStateNormal];
                    break;
                case 1:
                    [button setTitle:shopObject.shopHuoBi forState:UIControlStateNormal];
                    break;
                case 2:
                    [button setTitle:shopObject.shopHuoBi forState:UIControlStateNormal];
                    break;
                case 3:
                    [button setTitle:shopObject.shopHuoBi forState:UIControlStateNormal];
                    break;
                case 4:
                    [button setTitle:shopObject.shopHuoBi forState:UIControlStateNormal];
                    break;
                    
                default:
                    break;
            }
        }
        
    
    }     _shopObject=shopObject;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
